package com.spice.service.schedule;

import com.spice.service.entity.job.CleanupFileJob;
import com.spice.service.entity.job.JobExecutor;
import com.spice.service.entity.job.SaveRecordJob;
import com.spice.service.util.ServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ScheduledTasks {
    @Autowired
    private ServiceUtil serviceUtil;

    /**
     * 提交一次持久化任务记录的任务,无特殊情况,默认每十分钟执行一次。
     * 将任务记录从内存持久化到磁盘
     */
    @Scheduled(initialDelay = 1000, fixedRate = 1000 * 60 * 10)
    public static void saveDoneJobRecord() {
        log.info("开始持久化job");
        JobExecutor.addJob(new SaveRecordJob());
    }

    /**
     * 清理不再使用的文件，每周执行一次。
     */
    @Scheduled(initialDelay = 1000, fixedRate = 1000 * 60 * 60 * 24 * 7)
    public void cleanupFile() {
        log.info("开始清理多余文件");
        JobExecutor.addJob(new CleanupFileJob(serviceUtil));
    }


}
